﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Net;

namespace sp500
{
    class YahooDownloadFromIcharUrl
    {
        private string companysymbolTX;
        //private Sp500Dailyquote quter;
        //private List<Sp500Dailyquote> sp500DailyquotehistoryList;
        Sp500QuteHistoryList historydata = null;

        public YahooDownloadFromIcharUrl(string symbol)
        {
            companysymbolTX = symbol;
        }

        string DownLoadURL()
        {
            string yahooURLprefix = "http://ichart.yahoo.com/table.csv?s=";
            string returnDownLoadableURL = yahooURLprefix + companysymbolTX;
            return returnDownLoadableURL;
        }

        public void download()
        {
            using (WebClient web = new WebClient())
            {
                try
                {
                    CSVSaverLoader csvsaveloader = new CSVSaverLoader(companysymbolTX);
                    string historycallcsvdata;
                    const bool loadfromURL = false;
                    if (loadfromURL)
                    {
                        historycallcsvdata = web.DownloadString(DownLoadURL());
                        // create the related csv file permaterly
	                    
	                    csvsaveloader.savefile2csv(historycallcsvdata);
                    } 
                    else
                    {
                        historycallcsvdata = csvsaveloader.readcsvffile();
                    }

                    // parsing .csv data
                    HistoryCSVParsing csvparsing = new HistoryCSVParsing(historycallcsvdata, companysymbolTX);
                    csvparsing.perform();

                    historydata = csvparsing.getHistoryData();

                }
                catch (System.Exception ex)
                {
                    //ex.Message;
                    MessageBox.Show(ex.Message);
                }


            }
        }

        public Sp500QuteHistoryList getHistoryDataList()
        {
            return historydata;
        }


    }
}
